//
//  PGNavigationController.m
//  youqu
//
//  Created by 彭鹏 on 16/9/3.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGNavigationController.h"

@interface PGNavigationController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@end

@implementation PGNavigationController

+ (void)initialize {
    if ([PGNavigationController class] != self) return;
    
    UINavigationBar *navigationBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[self]];
    [navigationBar setTitleTextAttributes:@{
                                            NSFontAttributeName : [UIFont systemFontOfSize:18.0],
                                            NSForegroundColorAttributeName : [UIColor blackColor]}];
    
    [navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//     <UIScreenEdgePanGestureRecognizer: 0x7fd3bbdb0260; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fd3bbdaed60>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fd3bbd50ac0>)>>
//    NSLog(@"%@",self.interactivePopGestureRecognizer);
//    self.interactivePopGestureRecognizer.delegate = self;
    id panDelegate = self.interactivePopGestureRecognizer.delegate;
    self.interactivePopGestureRecognizer.delegate = nil;
    self.delegate = self;
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:panDelegate action:@selector(handleNavigationTransition:)];
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.childViewControllers.count > 0) {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [backButton setTitle:@"返回" forState:UIControlStateNormal];
        [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [backButton sizeToFit];
        [backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchDown];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    [super pushViewController:viewController animated:animated];
    
    
    
}

//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//    
//    NSLog(@"%zd",self.childViewControllers.count);
//    return self.childViewControllers.count > 1;
//}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return self.childViewControllers.count > 1;
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
#warning 调用2次  为什么
    //调用2次  为什么
//    NSLog(@"%@",NSStringFromClass([viewController class]));
//    NSLog(@"%s, line = %d", __FUNCTION__,__LINE__);
}

- (void)backButtonClick {
    [self popViewControllerAnimated:YES];
}



@end

























